
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports _2bMatesSite.BO

Namespace _2bMatesSite
	''' <summary>
	''' Summary description for delete_photo.
	''' </summary>
	Public Partial Class delete_photo
		Inherits BaseWebPage
		Protected isGroupMode As Boolean = False
		Protected grpID As Integer = -1
		Protected grp As Group = Nothing
		Protected htPageConstants As New Hashtable()

		Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
			' Put user code to initialize the page here
			If Request.Params("id") = Nothing OrElse Request.Params("id") = "" Then
				Throw New Exception(Constants.ERROR_MESSAGE_MISSING_PARAMETERS)
			End If
			Dim imgID As Integer

			' init page from XML
			Me.initPageTitles(Constants.DELETE_PHOTO, "", Form1, Nothing, htPageConstants)
			Me.initAdvertisements(Form1, Nothing)

			Try
				imgID = Convert.ToInt32(Request.Params("id"))
			Catch
				Throw New Exception(Constants.ERROR_MESSAGE_MISSING_PARAMETERS)
			End Try

			If Request.Params(Constants.GROUP_PARAM) <> Nothing AndAlso Request.Params(Constants.GROUP_PARAM) <> "" Then
				Try
					grpID = Convert.ToInt32(Request.Params(Constants.GROUP_PARAM))
				Catch
					Throw New Exception(Constants.ERROR_MESSAGE_MISSING_PARAMETERS)
				End Try

				isGroupMode = True
				grp = Groups.GetGroupByID(grpID)

				' check if Logged user is the moderator of the group
				If grp.Uid <> Me.LoggedUser.ID Then
					Throw New Exception(Constants.ERROR_MESSAGE_ONLY_MODERATOR_ACCESS)
				End If

				Dim grpPic As GroupPicture = grp.GetPicture(imgID)
				imgPhoto.ImageUrl = Pictures.GetPictureSrcFullURL(grpPic, False, "../")
			Else
				Dim pic As UserPicture = LoggedUser.GetPicture(imgID)
				imgPhoto.ImageUrl = Pictures.GetPictureSrcFullURL(pic, False, "../")
			End If

			If isGroupMode Then
				hlBackToGroup.NavigateUrl = "../" + Constants.GROUP_WELCOME + "?" + Constants.GROUP_PARAM + "=" + grpID
				hlBackToGroup.Text = (Me.GetPageConstText(htPageConstants, "CONST_GroupBackLink_TEXT")).Replace("[PARAM]", grp.Name)
			End If
			btnCancel.Attributes("onclick") = "javascript: history.back(); return false;"
		End Sub

		#region Web Form Designer generated code
		Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
			'
			' CODEGEN: This call is required by the ASP.NET Web Form Designer.
			'
			InitializeComponent()
			MyBase.OnInit(e)
		End Sub

		''' <summary>
		''' Required method for Designer support - do not modify
		''' the contents of this method with the code editor.
		''' </summary>
		Private Sub InitializeComponent()

		End Sub
#End Region

		Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs)
			Dim usr As User = LoggedUser
			Dim picID As Integer = Convert.ToInt32(Request.Params("id"))

			Try
				If isGroupMode Then
					deleteGroupPhoto(picID)
				Else
					deleteUserPhoto(picID)
				End If
			Catch generatedExceptionName As Exception
				Throw New Exception(Constants.ERROR_MESSAGE_UNEXPECTED_ERROR)
			End Try
		End Sub

		Private Sub deleteUserPhoto(ByVal picID As Integer)
			Dim usr As User = LoggedUser
			If picID = usr.DefaultPictureID Then
				Dim AllPics As ArrayList
				AllPics = usr.GetPictures()

				If AllPics.Count > 1 Then
					Dim curPic As UserPicture
					Dim i As Integer = 0
					While i < AllPics.Count
						curPic = DirectCast(AllPics(i), UserPicture)
						If curPic.ID <> usr.DefaultPictureID Then
							usr.DefaultPictureID = curPic.ID
							usr.Save()
							Exit While
						End If
						System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
					End While
				Else
					usr.DefaultPictureID = -1
					usr.Save()
				End If
			End If
			Dim pic As UserPicture = usr.GetPicture(picID)
			usr.DeletePicture(pic)
			Response.Redirect(Constants.UPLOAD_PHOTO)
		End Sub

		Private Sub deleteGroupPhoto(ByVal picID As Integer)
			Dim grp As Group = Groups.GetGroupByID(grpID)

			If picID = grp.DefaultPictureID Then
				Dim AllPics As ArrayList
				AllPics = grp.GetPictures()

				If AllPics.Count > 1 Then
					Dim curPic As GroupPicture
					Dim i As Integer = 0
					While i < AllPics.Count
						curPic = DirectCast(AllPics(i), GroupPicture)
						If curPic.ID <> grp.DefaultPictureID Then
							grp.DefaultPictureID = curPic.ID
							grp.Save()
							Exit While
						End If
						System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
					End While
				Else
					grp.DefaultPictureID = -1
					grp.Save()
				End If
			End If
			Dim pic As GroupPicture = grp.GetPicture(picID)
			grp.DeletePicture(pic)
			Response.Redirect(Constants.UPLOAD_GROUP_PHOTO + "?" + Constants.GROUP_PARAM + "=" + grpID)
		End Sub

		====================
        ' This public property was added by conversion wizard to allow the access of a protected, autogenerated member variable Form1.
		====================
        Public ReadOnly Property Form1() As System.Web.UI.HtmlControls.HtmlForm
            Get
                Return Migrated_Form1
            End Get
        End Property
        'set { Migrated_Form1 = value; }
    End Class
End Namespace






